$(function () {
    //如果是专业
    findSubjectList(function (result) {
        var parentList = result[0];
        if(parentList!=null && parentList.length>0){
            //设置专业内容
            var context = setSubjectHtml(parentList);
            $("#customized-major-list").html(context);
            $("#customized-major-list>li").click(function () {
                $(this).addClass('dc-active').siblings().removeClass('dc-active');
            });
            $("#next-step").click(function () {
                var parentSubject = $("#customized-major-list").find(".dc-active");
                if(parentSubject==null || typeof  parentSubject =='undefined'){
                    $.toast('请选择一个专业');
                    return;
                }
                $(".button-nav.pull-left").removeClass("goBack");
                $(".button-nav.pull-left").addClass("upBackTo");
                //隐藏父专业区
                $("#parent-subject-box").addClass("hide");
                //显示子专业区
                $("#child-subject-box").removeClass("hide");
                $(".upBackTo").click(function(){
                    $(".button-nav.pull-left").removeClass("upBackTo");
                    setTimeout(function(){
                        $(".button-nav.pull-left").addClass("goBack");
                    },500);
                    //显示子专业区
                    $("#child-subject-box").addClass("hide");
                    //隐藏父专业区
                    $("#parent-subject-box").removeClass("hide");
                });
                var subjectId = parentSubject.data('subject');
                var childList = result[subjectId];
                if(childList==null || childList.length <=0){
                    $("#child-customized-subject-list").html(notSubjectData());
                    return;
                }
                var childContext ='';
                $.each(childList,function (index,child) {
                    childContext+=' <li class="col-auto" data-subject="'+child.id+'"><a href="javascript:void(0)" class="button button-round">'+child.subjectName+'</a></li>'
                });
                $("#child-customized-subject-list").html(childContext);
                $("#child-customized-subject-list li").click(function () {
                    var _this = $(this);
                    if(_this.hasClass('active')){
                        _this.removeClass('active');
                    }else{
                        _this.addClass('active');
                    }
                });
                $("#customized-finished").click(function () {//完成定制
                    var currentLi = $("#child-customized-subject-list li.active");
                    if(currentLi==null || currentLi.length==0){
                        $.toast('请选择一个或多个科目');
                        return;
                    }
                    var childIds = currentLi.map(function (key,value) {
                        return $(value).data('subject');
                    }).get().join(",");
                    $.ajax({
                        url:baseloaction +'/mobile/user/setUserSubjectIds',
                        type:'post',
                        dataType:'json',
                        data:{
                            'sessionId':key,
                            'parentId':subjectId,
                            'childIds':childIds
                        },
                        success:function (result) {
                            if(result.success==true){
                                var _url = result.url;
                                if(_url==null || $.trim(_url)==''){
                                    window.location.href = baseloaction +'/mobile/uc/index';
                                }else{
                                    window.location.href =baseloaction + _url;
                                }
                            }else{
                                $.toast(result.message);
                            }
                        },
                        error:function (error) {
                            $.toast('完善专业数据错误，进入个人中心失败');
                        }
                    });
                });
            });
        }else{//如果没有数据
            $("#customized-major-list").html(notSubjectData());
        }
    });

    /**
     * 如果没有专业数据显示
     * @return {string}
     */
    function notSubjectData() {
        var context = '<li id="no-data-li"><section class="no-data__wrap"><em>&nbsp;</em><p>亲，暂无专业数据，小编正在整理中</p></section></li>';
        return context;
    }
    /**
     * 设置专业数据
     * @param subjectList
     * @return {*}
     */
    function setSubjectHtml(subjectList) {
        if(subjectList!=null && subjectList.length>0){
            var context = '';
            $.each(subjectList,function (index,subject) {
                if(index ==0){
                    context+='<li class="item-content dc-active" data-subject="'+subject.id+'">';
                }else{
                    context+='<li class="item-content" data-subject="'+subject.id+'">';
                }
                context+='<div class="item-inner">';
                context+='<div class="item-title">';
                context+='<span class="wm-cm-title">'+subject.subjectName+'</span>';
                context+='</div>';
                context+='<div class="item-after">';
                context+='<span class="dc-icon-bx ok-icon-bx dc-small-icon-bx"><em class="iconfont icon-duihao"></em></span>';
                context+='</div></div></li>';
            });
            return context;
        }
        return '';
    }

    $('body').on('click','.goBack',function(){
`            window.history.go(-1);
`    });
});